package utils

import (
	"github.com/spf13/cast"
	"math/rand"
)

type PassWord struct {
	UserName   string
	Salt       string
	Plaintext  string //明文
	Ciphertext string //密文
}

// PassWordCompareLogin 登录用 前端已经加过一次md5
func PassWordCompareLogin(plaintext, userName, salt, dbPassWold string) bool {
	//fmt.Println(Md5Lower([]byte(plaintext + userName + salt)))
	return Md5Lower([]byte(plaintext+userName+salt)) == dbPassWold
}

// Encode 加密
//md5(md5(明文)+盐)
func (pwd *PassWord) Encode() string {
	pwd.Ciphertext = Md5Lower([]byte(Md5Lower([]byte(pwd.Plaintext)) + pwd.UserName + pwd.Salt))
	return pwd.Ciphertext
}

// MakeSalt 加密
func (pwd *PassWord) MakeSalt() *PassWord {
	pwd.Salt = cast.ToString(rand.Intn(100000))
	return pwd
}
